<?php
namespace modules\contact;
use modules\auth\security;

class storage
{
    static function schemas(): array
    {
        return [
            'contacts' => [
                'id' => 'identity',
                'org' => '@orgs/org',
                'name' => 'string/64',
                'gender' => '*gender/man:woman',
                'email' => 'string/64',
                'phone1' => 'string/64',
                'addr1' => 'string/256',
                'phone2' => 'string/64',
                'addr2' => 'string/256',
                'active' => 'bool',
                'birthdate' => 'datetime',
                'comment' =>'string',
            ],
        ];
    }

    static function add($q, $name, $gender, $email, $phone1, $addr1, $phone2, $addr2, $birthdate, $comment, int $org)
    {
        return $q->table('contacts')
            ->field('name',
                    'gender',
                    'email',
                    'phone1',
                    'addr1',
                    'phone2',
                    'addr2',
                    'birthdate',
                    'comment',
                    'org',
                    'active')
                ->insert($name,
                         $gender,
                         $email,
                         $phone1,
                         $addr1,
                         $phone2,
                         $addr2,
                         $birthdate,
                         $comment,
                         $org,
                         true);
    }

    static function list_by_active($q, int $org, int $page, bool $active)
    {
        return $q->table('contacts')
            ->where(['org', $org])
            ->where(['active', $active])
            ->field('id', 'name', 'gender', 'phone1', 'addr1')
            ->select(10, $page);
    }


    static function profile($q, int $org, int $id)
    {
        return $q->table('contacts', $id)
            ->where(['org', $org])
            ->field('id',
                    'name',
                    'gender',
                    'email',
                    'phone1',
                    'addr1',
                    'phone2',
                    'addr2',
                    'birthdate',
                    'comment',
                    'org',
                    'active')
            ->select(0);
    }

    static function edit($q, $id, $name, $gender, $email, $phone1, $addr1, $phone2, $addr2, $birthdate, $comment, int $org)
    {
        return $q->table('contacts', $id)
            ->where(['org', $org])
            ->field('name', 'gender', 'email', 'phone1', 'addr1', 'phone2', 'addr2', 'birthdate', 'comment')
            ->update($name, $gender, $email, $phone1, $addr1, $phone2, $addr2, $birthdate, $comment);
    }

    static function active($q, int $org, int $id, bool $active)
    {
        return $q->table('contacts', $id)
            ->where(['org', $org])
            ->field('active')
            ->update($active);
    }
}

